import { useState } from 'react'
import { BasicDrawerProps } from '../index.type'

export function useDrawer(): [
  BasicDrawerProps['register'],
  { openDrawer: (values?: any) => void; closeDrawer: () => void }
] {
  const [visible, setVisible] = useState(false)
  const [initialValues, setInitialValues] = useState<any>()

  function openDrawer(initialValues?: any) {
    setInitialValues(initialValues)
    setVisible(true)
  }

  function closeDrawer() {
    setVisible(false)
  }

  function register(callback: (initialValues: any) => void) {
    visible && callback?.(initialValues)
    return { visible, initialValues, onClose: closeDrawer }
  }

  const drawerActions = {
    openDrawer,
    closeDrawer
  }

  return [register, drawerActions]
}
